Skip to content

Conversation

Andarist
Copy link
Contributor

@Andarist Andarist commented Oct 6, 2025

fixes #62552

@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Oct 6, 2025
@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Oct 6, 2025
@RyanCavanaugh
Copy link
Member

How does this affect this sample?

function id<T extends { foo : 0 | 1 }>(x: T): T {
  return x
}
const Foo = id(class {
  static readonly foo = 0
});

@Andarist
Copy link
Contributor Author

Andarist commented Oct 6, 2025

It has no effect on this sample. This one works before and after this PR. The more interesting case would be a similar one with a mutable property:

function id<T extends { foo : 0 | 1 }>(x: T): T {
  return x
}
const Foo = id(class {
  static foo = 0
});

This one errors before and after this PR as 0 widens to number. In general, the contextual typing for those static properties is very limited in inference contexts. The problem is that those properties are not checked from within the scope of checkExpressionWithContextualType so when the constraint assignability check in getInferredType does its thing the contextual type used for those static properties is any (coming from getContextualTypeForArgumentAtIndex as signature === resolvingSignature).

When there is no constraint (like in the reported issue) the class expression is checked in a deferred fashion (outside of the inference context) and then the contextual type becomes the class expression itself (coming from the already inferred and resolved signature).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

Incorrect report of self-referencing type for static fields
3 participants